/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LocationForm;
import com.ntb.entities.Locations;
import com.ntb.models.LocationsDAO;
import com.ntb.models.impl.LocationsDAOImpl;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class CostHistoryAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionForward forward = mapping.getInputForward();
        LocationForm locationForm = (LocationForm) form;
        LocationsDAO locationDao = new LocationsDAOImpl();

        if (locationForm.getLocationID() > 0) {
            Locations location = locationDao.getRow(locationForm.getLocationID());

            locationForm.setLocationID(location.getLocationID());
            locationForm.setLocationName(location.getLocationName());
            locationForm.setPresentCost("" + location.getPresentCost());

            List yearList = locationDao.getCostHistoryYear(locationForm.getLocationID());

            request.setAttribute("yearList", yearList);

            String year = "";
            try {
                if (locationForm.getYear() != null && !"".equals(locationForm.getYear())) {
                    year = locationForm.getYear();
                } else {
                    year = yearList.get(0).toString();
                }
            } catch (Exception ex) {
            }
            locationForm.setYear(year);

            request.setAttribute("selectYear", year);

            request.setAttribute("locationName", locationForm.getLocationName());

            List costHistory = locationDao.getCostHistory(locationForm.getLocationID(), year);
            request.setAttribute("costHistory", costHistory);
        }


        return forward;
    }

}
